]> git.r.bdr.sh - rbdr/super-polarity/blob - Super Polarity/Actors/MainShip.cs
Implements polarity system
[rbdr/super-polarity] / Super Polarity / Actors / MainShip.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using Microsoft.Xna.Framework;
6 using Microsoft.Xna.Framework.Content;
7 using Microsoft.Xna.Framework.Graphics;
8
9 namespace SuperPolarity
10 {
11 class MainShip : Ship
12 {
13
14 uint Multiplier;
15 uint Lives;
16 uint Score;
17 ParticleEngine particleEngine;
18
19 public MainShip(Game newGame) : base(newGame) {}
20
21 public override void Initialize(Texture2D texture, Vector2 position)
22 {
23 base.Initialize(texture, position);
24
25 InitParticleEngine();
26 SetPolarity(Polarity.Positive);
27
28 Multiplier = 1;
29 Lives = 3;
30 Score = 0;
31
32 BindInput();
33 }
34
35 void InitParticleEngine()
36 {
37 List<Texture2D> texturesList = new List<Texture2D>();
38 texturesList.Add(game.Content.Load<Texture2D>("Graphics\\circle"));
39 texturesList.Add(game.Content.Load<Texture2D>("Graphics\\diamond"));
40 texturesList.Add(game.Content.Load<Texture2D>("Graphics\\star"));
41
42 particleEngine = new ParticleEngine(texturesList, Position);
43 }
44
45 void BindInput()
46 {
47 InputController.Bind("moveX", HandleHorizontalMovement);
48 InputController.Bind("moveY", HandleVerticalMovement);
49 InputController.Bind("changePolarity", HandleChangePolarity);
50 }
51
52 protected void HandleChangePolarity(float value)
53 {
54 SwitchPolarity();
55 }
56
57 public void HandleHorizontalMovement(float value)
58 {
59 Acceleration.X = value * AccelerationRate;
60
61 if (value > 0.1 && Velocity.X < 0 || value < 0.1 && Velocity.X > 0)
62 {
63 Acceleration.X *= 2;
64 }
65
66 if (value > 0.1 && Velocity.Y < 0 || value < 0.1 && Velocity.Y > 0)
67 {
68 Acceleration.Y *= 2;
69 }
70 }
71
72 public void HandleVerticalMovement(float value)
73 {
74 Acceleration.Y = value * AccelerationRate;
75 }
76
77 public override void SwitchPolarity()
78 {
79 base.SwitchPolarity();
80 SwitchParticleEngine(CurrentPolarity);
81 }
82
83 public override void SetPolarity(Polarity newPolarity)
84 {
85 base.SetPolarity(newPolarity);
86 SwitchParticleEngine(newPolarity);
87 }
88
89 protected void SwitchParticleEngine(Polarity polarity)
90 {
91 if (polarity == Polarity.Positive)
92 {
93 particleEngine.Color = Color.Red;
94 }
95 else if (polarity == Polarity.Negative)
96 {
97 particleEngine.Color = Color.Blue;
98 }
99 else
100 {
101 particleEngine.Color = Color.Gray;
102 }
103 }
104
105 public override void Update(GameTime gameTime)
106 {
107 base.Update(gameTime);
108 particleEngine.EmitterLocation = Position;
109 particleEngine.Update();
110 ConstrainToEdges();
111 }
112
113 public override void Magnetize(Ship ship, float distance, float angle)
114 {
115 }
116
117 protected void ConstrainToEdges()
118 {
119 if (Position.X < 0)
120 {
121 Position.X = 0;
122
123 if (Velocity.X < 0)
124 {
125 Velocity.X = 0;
126 }
127 }
128 if (Position.X > game.GraphicsDevice.Viewport.Width)
129 {
130 Position.X = game.GraphicsDevice.Viewport.Width;
131
132 if (Velocity.X > 0)
133 {
134 Velocity.X = 0;
135 }
136 }
137 if (Position.Y < 0)
138 {
139 Position.Y = 0;
140
141 if (Velocity.Y < 0)
142 {
143 Velocity.Y = 0;
144 }
145 }
146 if (Position.Y > game.GraphicsDevice.Viewport.Height)
147 {
148 Position.Y = game.GraphicsDevice.Viewport.Height;
149
150 if (Velocity.Y < 0)
151 {
152 Velocity.Y = 0;
153 }
154 }
155 }
156
157 public override void Draw(SpriteBatch spriteBatch)
158 {
159 particleEngine.Draw(spriteBatch);
160 base.Draw(spriteBatch);
161 }
162 }
163 }